layui.use(['form', 'upload', 'laydate'] ,function() {

    var form = layui.form
        , $ = layui.jquery
        , layer = layui.layer
        , upload = layui.upload
        , laydate = layui.laydate;

    //日期
    laydate.render({
        elem: '#date'
    });

    //普通图片上传
    var uploadInst = upload.render({
        elem: '#test1'
        , url: '/fr/uploadFile'
        , before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#demo1').attr('src', result); //图片链接（base64）
            });
        }
        , done: function (res) {
            if (res.code > 0) {
                return layer.msg('上传成功');
            }
            return layer.msg('上传失败');
        }
        , error: function () {
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }
    });
})

$(function () {
    $('#selfSubmit').click(function () {
        let username = $("input[name='username']").val();
        let headPicture = $("input[name='headPicture']").val();
        let telNumber = $("input[name='usertelNumber']").val();
        let province = $("#province").prop("selected", true).val();
        let city = $("#city").prop("city", true).val();
        let area = $("#area").prop("area", true).val();
        let flavorId = $("#flavorId").prop("flavorId", true).val();
        let leftConsum = $("#leftConsum").val();
        let rightConsum = $("#rightConsum").val();
        let signature = $("#signature").val();
        let form_data = {
            username:username,
            headPicture:headPicture,
            telNumber:telNumber,
            province:province,
            city:city,
            area:area,
            flavorId:flavorId,
            leftConsum:leftConsum,
            rightConsum:rightConsum,
            signature:signature
        };
        $.ajax({
            type: 'post',
            url: '/fr/personal/personalUpdate',
            contentType: 'application/json',
            dataType: 'json',
            data: JSON.stringify(form_data),
            success: function () {
                $("#selfInfoForm").submit();
            }
        })
    })
})

$(function () {
    $.ajax({
        type: 'post',
        url: '/fr/personal/collections/1',
        success:function (data) {
            let list = eval(data.data);
            console.log(list);
            let str ="";
            let page = "";
            let Page = "";
            $.each(list,function (index,item) {
                str += "<div id=\"collections"+index+"\" class=\"contact-box\">\n" +
                    "\n" +
                    "                    <div class=\"col-md-1\">\n" +
                    "                        <div class=\"text-center\">\n" +
                    "                            <img alt=\"image\" class=\"img-circle m-t-xs img-responsive\" src=\"/fr"+item.shopPicture+"\">\n" +
                    "                        </div>\n" +
                    "                    </div>\n" +
                    "                    <div class=\"col-md-3\">\n" +
                    "                        <h3><strong>"+item.shopName+"</strong></h3>\n" +
                    "                        <p><i class=\"fa fa-map-marker\"></i> "+item.province+""+item.city+""+item.area+"</p>\n" +
                    "                        <address>\n" +
                    "                            营业时间: "+item.businessTime+"<br>\n" +
                    "                            <abbr title=\"Phone\">电话号码:</abbr> "+item.shopTelnumber+"\n" +
                    "                        </address>\n" +
                    "                    </div>\n" +
                    "                    <div class=\"col-md-4\"> <br> <strong>店铺简介</strong><br> \n" +
                    "                     <strong class=\"introduction\">人均消费约为"+item.consumption+"</strong><br><strong>该店铺收藏于"+item.createDate+"</strong></div>\n" +
                    "                    <div class=\"col-md-2\">\n" +
                    "                        <div class=\"text-center\">\n" +
                    "                            <br>\n" +
                    "                    <form action='/fr/personal/cancel/"+item.shopId+"/"+item.userId+"' method='post'>" +
                    "                           <button type='submit' class=\"btn btn-w-m btn-danger col-md-8\">取消收藏</button></form>\n" +
                    "                            <br><br>\n" +
                    "                            <a href='/fr/storeInfo/enter/"+item.shopId+"' class=\"btn btn-w-m btn-default col-md-8\">查看店铺</a>\n" +
                    "                        </div>\n" +
                    "                    </div>\n" +
                    "                    <div class=\"clearfix\"></div>\n" +
                    "                </div>";

                page="<div class=\"container\" style=\"text-align: center\">" +
                    "<ul class=\"pagination\">";
                if (item.count >= 2){
                    page += "<li><a href=\"javascript:void(0);\" onclick=\"js_method("+(item.count-1)+")\">&laquo;</a></li>";
                }else {
                    page += " <li class=\"disabled\"><a>&laquo;</a></li>";
                }
                for (let i = item.count; i <= (item.page<6?item.page:(item.count+5)); i++) {
                    if (i==item.count){
                        page += "<li class=\"active\" style='background-color: #1ab394;'><a>"+i+"</a></li>";
                    }else {
                        page += "<li><a href=\"javascript:void(0);\" onclick=\"js_method("+i+")\">"+i+"</a></li>"
                    }
                }
                if (item.count < item.page){
                    page += "<li><a href=\"javascript:void(0);\" onclick=\"js_method("+(item.count+1)+")\">&raquo;</a></li>";
                }

                page+="     <li><a>当前页"+item.count+"/"+item.page+"总页数</a></li>" +
                    "   </ul>" +
                    "</div>";
                Page = page;
            })
            $("#collection").html(str+Page);
        }
    })
});

function js_method(page) {
    $.ajax({
        type: 'post',
        url: '/fr/personal/collections/'+page+'',
        success:function (data) {
            let list = eval(data.data);
            console.log(list);
            let str ="";
            let page = "";
            let Page = "";
            $.each(list,function (index,item) {
                str += "<div id=\"collections"+index+"\" class=\"contact-box\">\n" +
                    "\n" +
                    "                    <div class=\"col-md-1\">\n" +
                    "                        <div class=\"text-center\">\n" +
                    "                            <img alt=\"image\" class=\"img-circle m-t-xs img-responsive\" src=\"/fr"+item.shopPicture+"\">\n" +
                    "                        </div>\n" +
                    "                    </div>\n" +
                    "                    <div class=\"col-md-3\">\n" +
                    "                        <h3><strong>"+item.shopName+"</strong></h3>\n" +
                    "                        <p><i class=\"fa fa-map-marker\"></i> "+item.province+""+item.city+""+item.area+"</p>\n" +
                    "                        <address>\n" +
                    "                            营业时间: "+item.businessTime+"<br>\n" +
                    "                            <abbr title=\"Phone\">电话号码:</abbr> "+item.shopTelnumber+"\n" +
                    "                        </address>\n" +
                    "                    </div>\n" +
                    "                    <div class=\"col-md-4\"> <br> <strong>店铺简介</strong><br> \n" +
                    "                     <strong class=\"introduction\">人均消费约为"+item.consumption+"</strong><br><strong>该店铺收藏于"+item.createDate+"</strong></div>\n" +
                    "                    <div class=\"col-md-2\">\n" +
                    "                        <div class=\"text-center\">\n" +
                    "                            <br>\n" +
                    "                    <form action='/fr/personal/cancel/"+item.shopId+"/"+item.userId+"' method='post'>" +
                    "                           <button type='submit' class=\"btn btn-w-m btn-danger col-md-8\">取消收藏</button></form>\n" +
                    "                            <br><br>\n" +
                    "                            <a href='/fr/storeInfo/enter/"+item.shopId+"' class=\"btn btn-w-m btn-default col-md-8\">查看店铺</a>\n" +
                    "                        </div>\n" +
                    "                    </div>\n" +
                    "                    <div class=\"clearfix\"></div>\n" +
                    "                </div>";

                page="<div class=\"container\" style=\"text-align: center\">" +
                    "<ul class=\"pagination\">";
                if (item.count >= 2){
                    page += "<li><a href=\"javascript:void(0);\" onclick=\"js_method("+(item.count-1)+")\">&laquo;</a></li>";
                }else {
                    page += " <li class=\"disabled\"><a>&laquo;</a></li>";
                }
                for (let i = item.count; i <= (item.page<6?item.page:(item.count+5)); i++) {
                    if (i==item.count){
                        page += "<li class=\"active\" style='background-color: #1ab394;'><a>"+i+"</a></li>";
                    }else {
                        page += "<li><a href=\"javascript:void(0);\" onclick=\"js_method("+i+")\">"+i+"</a></li>"                        }
                }
                if (item.count < item.page){
                    page += "<li><a href=\"javascript:void(0);\" onclick=\"js_method("+(item.count+1)+")\">&raquo;</a></li>";
                }
                page+="     <li><a>当前页"+item.count+"/"+item.page+"总页数</a></li>" +
                    "   </ul>" +
                    "</div>";
                Page = page;
            })
            $("#collection").html(str+Page);
        }
    })
}

function getUrlParam(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    let r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
}
$(function () {
    let page = getUrlParam('page');
    $('#myTab li:eq('+page+') a').tab('show');
});